package com.jkhan.common.result;

import lombok.Data;

/**
 * @ClassName ResponseEntity
 * @Description
 * @Author hjk
 * @date 2023/3/4 16:33
 * @Version 1.0
 */
@Data
public class ResponseEntity<T> {

    private Integer code;

    private String msg;

    private T data;

    protected static <T> ResponseEntity<T> build(T data) {
        ResponseEntity<T> result = new ResponseEntity<>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> ResponseEntity<T> build(T body, Integer code, String message) {
        ResponseEntity<T> result = build(body);
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

    public static <T> ResponseEntity<T> build(ResponseEnum resultCodeEnum) {
        ResponseEntity<T> result = build(null);
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMsg());
        return result;
    }

    public static <T> ResponseEntity<T> build(T body, ResponseEnum resultCodeEnum) {
        ResponseEntity<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMsg());
        return result;
    }

    public static <T> ResponseEntity<T> ok() {
        return ResponseEntity.ok(null);
    }

    public static <T> ResponseEntity<T> ok(T data) {
        return build(data, ResponseEnum.SUCCESS);
    }

    public static <T> ResponseEntity<T> fail() {
        return ResponseEntity.fail(null);
    }

    public static <T> ResponseEntity<T> fail(T data) {
        return build(data, ResponseEnum.FAILURE);
    }

    public static <T> ResponseEntity<T> error(T data) {
        return build(data, ResponseEnum.ERROR);
    }

    public static <T> ResponseEntity<T> error() {
        return error(null);
    }

    public ResponseEntity<T> msg(String msg){
        this.setMsg(msg);
        return this;
    }

    public ResponseEntity<T> code(Integer code){
        this.setCode(code);
        return this;
    }

}
